package com.yuqing.sentimentapp.yqlist.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.linkface.baselibrary.utils.DateUtil;
import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.common.adaper.CommonRecycleAdapter;
import com.linkface.common.adaper.CommonViewHolder;
import com.linkface.common.adaper.MultiTypeSupport;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.utils.DisplayUtil;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.widget.jzvideo.Jzvd;
import com.yuqing.sentimentapp.widget.jzvideo.JzvdStd;
import com.yuqing.sentimentapp.yqlist.bean.YuQingDataBean;

import java.util.List;

import wseemann.media.FFmpegMediaMetadataRetriever;

/**
 * Created by shenhe on 2018/9/19.
 */

public class YqListAdapter extends CommonRecycleAdapter<YuQingDataBean> implements MultiTypeSupport<YuQingDataBean> {
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private Context mContext;

    public YqListAdapter(Context context, List<YuQingDataBean> dataList) {
        super(context, dataList, R.layout.item_fragment_yqlist_no_pic);
        this.mContext = context;
    }

    public YqListAdapter(Context context, List<YuQingDataBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.item_fragment_yqlist_no_pic);
        this.mContext = context;
        multiTypeSupport = this;
        this.commonClickListener = commonClickListener;
    }

    @Override
    public void bindData(final CommonViewHolder holder, final YuQingDataBean data) {
        String isRead = data.getIsRead();
        if (!TextUtils.isEmpty(isRead) && TextUtils.equals("1", isRead)) {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_333333));
        }
        String caption = data.getCaption();
        if (TextUtils.isEmpty(caption)) caption = "";
        holder.setText(R.id.tv_title,caption + data.getTitle())
        .setText(R.id.tv_source, data.getSource())
        .setText(R.id.tv_date_time, DateUtil.formatDateTime(data.getDatatime()))
        .setText(R.id.tv_sub_status, data.getSubStatus());
        if (TextUtils.isEmpty(data.getSentiment())) {
            holder.getView(R.id.tv_positive).setVisibility(View.GONE);
        } else {
            switch (data.getSentiment()) {
                case "A":
                    holder.setText(R.id.tv_positive, "正面");
                    break;
                case "B":
                    holder.setText(R.id.tv_positive, "负面");
                    break;
                case "C":
                    holder.setText(R.id.tv_positive, "敏感");
                    break;
            }
            holder.getView(R.id.tv_positive).setVisibility(View.VISIBLE);
        }
        //是否是视频
        if (data.isVideo()) {
            JzvdStd jzvdStd = holder.getView(R.id.videoplayer);
            jzvdStd.setUp(data.getVideoUrl(), data.getTitle(), Jzvd.SCREEN_NORMAL);
            if (TextUtils.isEmpty(data.getVideoThumb())) {
                FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
                try {
                    fmmr.setDataSource(data.getVideoUrl());
                    Bitmap bitmap = fmmr.getFrameAtTime();
                    if (bitmap != null) {
                        Bitmap b2 = fmmr
                                .getFrameAtTime(
                                        4000000,
                                        FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                        if (b2 != null) {
                            bitmap = b2;
                        }
                        if (bitmap.getWidth() > 640) {// 如果图片宽度规格超过640px,则进行压缩
                            bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                                    DisplayUtil.getScreenWidth(mContext) - DensityUtil.dip2px(mContext, 40), DensityUtil.dip2px(mContext, 200),
                                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                        }
                        jzvdStd.thumbImageView.setImageBitmap(bitmap);
                    }
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                } finally {
                    fmmr.release();
                }
            } else {
                GlideUtil.getImageWithErrorLoadingImg(mContext, data.getVideoThumb(), jzvdStd.thumbImageView, R.drawable.ic_default_big, R.drawable.ic_default_big);
            }
        } else {
            String imageUrl = data.getImageUrl();
            if (!TextUtils.isEmpty(imageUrl)) {
                String[] images = imageUrl.split(";");
                if (images.length > 0 && images.length < 3){//一张图
                    GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic), 3);
                } else if (images.length >= 3){//三张图
                    GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic_one), 3);
                    GlideUtil.loadRoundImage(mContext, images[1], holder.getView(R.id.iv_pic_two), 3);
                    GlideUtil.loadRoundImage(mContext, images[2], holder.getView(R.id.iv_pic_three), 3);
                }
            }
        }
        holder.setCommonClickListener(commonClickListener);
    }

    @Override
    public int getLayoutId(YuQingDataBean item, int position) {
        if (item.isVideo()) return R.layout.item_videoview_yqlist;
        String imageUrl = item.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return R.layout.item_fragment_yqlist_no_pic;
        }
        String[] images = imageUrl.split(";");
        if (images.length == 0) {
            return R.layout.item_fragment_yqlist_no_pic;
        } else if (images.length < 3){
            return R.layout.item_fragment_yqlist_one_pic;
        } else {
            return R.layout.item_fragment_yqlist_three_pic;
        }
    }

}
